baselib.report(0x00, "TD4.5.5 beginning.")
-- 下面是初始化操作:
busilib.SetOrientation(1)             -- 设置USB的CC线,另一根则为Vconn线(这里设置CC1)
-- 第一步:CVS transitions to Unattached.SNK
busilib.setUsbCcs(CCS.Unattached)               
busilib.setUsbRole(ROLE.SNK)
-- 第二步:CVS waits 5ms
baselib.delay(5)
-- 第三步:CVS verifies:
-- 3.a步:PUT presents Rp within tDRP – dcSRC.DRP x tDRP [TD.4.5.5.V.1]
-- PUT在tDRP – dcSRC.DRP x tDRP时间内呈现Rp
baselib.delay(30)                     -- 等待tDRP(100ms) - dcSRC.DRP(0.7) x tDRP(100ms)
local VCC1 = busilib.GetADC(ADC.CC1, 4)
if VCC1 > 200 then
    baselib.report(0x00, "TD.4.5.5.V.1 pass.")
else
    baselib.report(0x00, "TD.4.5.5.V.1 fail.")
end
-- 3.b步:PUT transitions from Rd to Rp within tDRPTransition [TD.4.5.5.V.8]
-- PUT在tDRPTransition时间内从Rd转换到Rp
-- ？？？？？上面不是已经是Rp了吗？？？？？
-- 3.c步:PUT transitions to Attached.SRC after tCCDebounce:PUT sources Vbus within tVbusON. [TD.4.5.5.V.9]
-- PUT在tCCDebounce后转换到Attached.SRC：PUT在tVbusON时间内提供Vbus
baselib.delay(275)               -- tVBUSON:275ms
if cstmlib.VBusVerified(1) then 
    baselib.report(0x00, "TD4.5.5.V.9 pass.")
else
    baselib.report(0x00, "TD4.5.5.V.9 fail.")
end
-- 第四步:CVS transitions to Try.SRC
-- 目前好像没有这个状态
busilib.setUsbRole(ROLE.SRC)
-- 第五步:CVS verifies that PUT transitions to Unattached.SNK within tSRCDisconnect [TD.4.5.5.V.2]
baselib.delay(20)               -- 等待tSRCDisconnect(20ms)
local VBUS = busilib.GetADC(ADC.VBUS, 4)          
baselib.report(0x00, "VBUS is " .. VBUS .. "mV.")
VCC1 = busilib.GetADC(ADC.CC1, 4)
local VCC2 = busilib.GetADC(ADC.CC2, 4)
baselib.report(0x00, "CC1 is " .. VCC1 .. "mV, and CC2 is " .. VCC2 .. "mV.")
if VCC1 > 275 or VCC2 > 275 or VBUS > 0.8 then      -- 这里通过监测CC1,CC2和VBUS来判断是否转换到Unattached.SNK状态
    baselib.report(0x00, "TD4.5.5.V.2 fail.")
else
    baselib.report(0x00, "TD4.5.5.V.2 pass.")
end
-- 第六步:CVS transitions to Attached.SRC after min tPDDebounce
baselib.delay(10)               -- 等待min tPDDebounce(10ms)
busilib.setUsbCcs(CCS.Attached)               
-- 第七步:CVS verifies that PUT transitions to Attached.SNK:
--[[7.a步:监测VBUS电流,for 500ms,电流数值可以是20ms内的平均值,判别不超过500mA(对应tester宣传的500mA供电能力)         
(原文:PUT sinks current according to CVS advertisement [TD.4.5.5.V.3]) ]]
local trans1 = busilib.newTrans4ad(4, ADC.CC2, {2750})
--这里对上面的trans1的状态1监控,6.d的这两行代码写前面是因为需要提前启动监控器
local monitor3 = busilib.newMonitor(TMD.CHECK_POINT, trans1, {1}, {0}, {0})
if busilib.waitCondition4ad(ADC.VBUS_I, 20, 500, 5000, 1, 500) == LuaOk then
    baselib.report(0x00, "TD.4.5.5.V.3 fail.")         --出现LuaOK说明有大于500mA的电流出现了一次,检查点fail
else
    baselib.report(0x00, "TD.4.5.5.V.3 pass.")
end
-- 7.b步:If PUT supports PD, CVS and PUT establish a PD contract. [TD.4.5.5.V.4]
-- 如果PUT支持PD，CVS和PUT协商PD协议
if USB_PD_Support then
    baselib.report(0x00, "Begin power negotiation.")
    tcpclib.SetPermission(1, 2)
    PN_result = tcpclib.PresetPowerNegotiation(1, 500)  --timeout is 500ms
    if PN_result == LuaOk then
        baselib.report(0x00, "TD4.5.5.V.4 pass.")
    else
        if PN_result == LuaTimeout then
            baselib.report(0x00, "Power negotiation timeout.")
        else
            baselib.report(0x00, "Unsupported power request received.")
        end
        baselib.report(0x00, "TD4.5.5.V.4 fail.")
    end
end
-- 7.d步:Else if PUT supports USB 2.0 UFP:PUT starts data communications on its D+/D- pair. [TD.4.5.5.V.6]
-- 不会写,先空着
--删除监控器
busilib.deleteMonitor(monitor3)
busilib.deleteTrans(trans1)
-- 第八步:CVS transitions to Disabled
busilib.setUsbCcs(CCS.Disabled)
-- 第九步:CVS verifies that PUT transitions to Unattached.SNK before tVBUSOFF expires:PUT data communication has ceased. [TD.4.5.5.V.7]
-- 不会写,先空着